<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<script type="text/javascript">
Ext.namespace('catalog.catalog.absenceSelect');
catalog.catalog.absenceSelect.SearchPanel = Ext.extend(catalog.ComplexGrid, {
        addAbs:'Adauga absenta',
        modifyAbs:'Motiveaza absenta',
        comment:'Comentariu',
        date:'Data',
        motivata:'Tip absenta',

    initComponent:function() { 
        searchURL = 'displayAbsencesByStudent.action?studentId=' +<%=request.getParameter("studentId")%>;
        var config = {
			compSet:[{url:0,reader:0,columns:0,bbarAction:0}],
			searchFields :[[
                            'Absente pana la data:', {xtype:'datefield', name:'absenceDTO.date', width:120}
                            ]],
            urls: [searchURL],
	        readers : [[
	            {name: 'date'},
	            {name: 'comment'},
	            {name: 'motivata'}
	        ]],
            columnsArray: [[
                {header: this.date, width: 100, sortable: true, dataIndex: 'date'},
                {header: this.comment, width: 100, sortable: true, dataIndex: 'comment'},
                {header: this.motivata, width: 180, sortable: true, dataIndex: 'motivata',
                 editor: new Ext.form.ComboBox({
                      typeAhead: true,
                      triggerAction: 'all',
                      lazyRender:true,
                      name:'absenceState',
                      fieldLabel:this.valueText,
                      mode: 'local',
                      store: new Ext.data.ArrayStore({
                      id: 0,
                      fields: [
                      'myId',
                      'displayText'
                      ],
                      data: [[1, 'Nemotivata'], [2, 'Motivata']]
                      }),
                      valueField: 'myId',
                      displayField: 'displayText',
                      value:1
                      })    }

	        ]],
	        bbarActions:[[{
                text:this.modifyAbs,
                iconCls :'edit-icon',
                scope:this,
                handler:this.updateAbsenceFun
               }]],
            tbarActions : [{
                text:this.addAbs,
                iconCls :'add-icon',
                scope:this,
                handler:this.addAbsFun
            }]

        }; // eo config object
        Ext.apply(this, Ext.apply(this.initialConfig, config)); 
        catalog.catalog.absenceSelect.SearchPanel.superclass.initComponent.apply(this, arguments);
    },
    addAbsFun :function() {
        var URL = 'catalog/adaugaAbsenta.jsp?studentId=' +<%=request.getParameter("studentId")%>;
        var win = catalog.util.genWindow({
            id: 'addAbsenceWindow',
            title    : this.addAbs,
            autoLoad : {url:URL,scripts:true},
            width    : 500,
            height   : 360
        });
    },
    updateAbsenceFun :function() {
      var absence = this.selModel.selections.keys;
                      if (absence.length !=1) {
                      Ext.MessageBox.alert(mbLocale.infoMsg, 'Trebuie sa alegeti o singura absenta!');
                      return;
                      }
                      var submitFun = function(buttonId, text, opt) {
                      if (buttonId == 'yes') {
                      catalog.util.ajaxRequest({
                      url: 'modifyAbsence.action',
                      params:{'absence.id':absence},
                      success:function(r,o){
                      var res = Ext.decode(r.responseText);
                      if (res && res.success){
                      this.store.reload();
                      }
                      },
                      failure:Ext.emptyFn
                      },this);
                      }
                      };
                      Ext.Msg.show({
                      title:mbLocale.infoMsg,
                      msg: 'Sunteti sigur ca doriti sa motivati absenta selectata?',
                      buttons: Ext.Msg.YESNO,
                      scope: this,
                      fn: submitFun,
                      icon: Ext.MessageBox.QUESTION
                      });
    }
});


try {absenceSelectLocale();} catch (e) {}

Ext.onReady(function() {
    Ext.QuickTips.init();
    var formPanel = new catalog.catalog.absenceSelect.SearchPanel();
    formPanel.store.reload();
    Ext.WindowMgr.getActive().add(formPanel);
    Ext.WindowMgr.getActive().doLayout();    
});
</script>